home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK2.toast / Development Kits (Disc 2) / Thread Manager / Thread Manager 2.1.1d1+ / ThreadedSort / Sprocket / Lib / AppleEventHandling.cp next >
Encoding:
Text File  |  1995-04-28  |  8.5 KB  |  292 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        AppleEventHandling.cp
  3.  
  4.     Contains:    Minimalist support for the required and Display Manager AppleEvents
  5.                 We also support openning and printing “LetterSpec” documents for AOCE.
  6.                 
  7.                 To really support AppleScript™, we’ll need to do ALOT more work in here.
  8.  
  9.     Written by: Dave Falkenburg
  10.  
  11.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  12.  
  13.     Change History (most recent first):
  14.      
  15.          <5>    11/16/94    DRF        Add StandardAEIdleProc for people who like using AESend with
  16.                                     kAEWaitReply.
  17.          <4>    11/12/94    DRF        Removed extra #include.
  18.          <3>     9/27/94    DRF         AppLib.h is now Sprocket.h
  19.          <2>      9/4/94    DRF        Added stub Text Services AppleEvent handlers.
  20.  */
  21.  
  22. #include "Sprocket.h"
  23.  
  24. #include <AppleEvents.h>
  25. #include <Errors.h>
  26. #include <Displays.h>            //    for Display Manager AppleEvent constants
  27. #include <OCEStandardMail.h>    //    for LetterSpec
  28.  
  29. #if    qInlineInputAware
  30. #include <TextServices.h>
  31. #endif
  32.  
  33. #include "AppleEventHandling.h"
  34.  
  35. static pascal Boolean StandardAEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn);
  36.     
  37.  
  38. void
  39. InstallAppleEventHandlers(void)
  40.     {
  41.     //    It’s probably more efficient to use a table to install these handlers…
  42.     
  43.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,NewAEEventHandlerProc(HandleOpenApplication),0,false);
  44.     (void) AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,NewAEEventHandlerProc(HandleOpenDocuments),0,false);
  45.     (void) AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,NewAEEventHandlerProc(HandlePrintDocuments),0,false);
  46.     (void) AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,NewAEEventHandlerProc(HandleQuitApplication),0,false);
  47.  
  48.     //    regardless of whether or not we have the display manager, go ahead and register an AppleEvent handler
  49.     (void) AEInstallEventHandler(kCoreEventClass,kAESystemConfigNotice,NewAEEventHandlerProc(HandleSystemConfigNotice),0,false);
  50.  
  51. #if    qInlineInputAware
  52.     if (gHasTextServices)
  53.         {
  54.         (void) AEInstallEventHandler(kTextServiceClass,kUpdateActiveInputArea,NewAEEventHandlerProc(HandleTextServicesUpdateActiveInputArea),0,false);
  55.         (void) AEInstallEventHandler(kTextServiceClass,kPos2Offset,NewAEEventHandlerProc(HandleTextServicesPos2Offset),0,false);
  56.         (void) AEInstallEventHandler(kTextServiceClass,kOffset2Pos,NewAEEventHandlerProc(HandleTextServicesOffset2Pos),0,false);
  57.         //    we don’t need to handle <kTextServiceClass,kShowHideInputWindow> events
  58.         }
  59. #endif
  60.     }
  61.  
  62.  
  63. OSErr
  64. CheckAppleEventForMissingParams(AppleEvent *theAppleEvent)
  65.     {
  66.     DescType    returnedType;
  67.     Size        actualSize;
  68.     OSErr        err;
  69.     
  70.     err = AEGetAttributePtr(theAppleEvent,keyMissedKeywordAttr,typeWildCard,
  71.                             &returnedType,nil,0,&actualSize);
  72.     
  73.     if (err == errAEDescNotFound)        //    If we couldn’t find the error attribute
  74.         return noErr;                    //        everything is ok, return noErr
  75.     
  76.     if (err == noErr)                    //    We found an error attribute, so
  77.         return errAEEventNotHandled;    //        tell the client we ignored the event
  78.  
  79.     return err;                            //    Something else happened, return it
  80.     }
  81.  
  82.  
  83. AEIdleUPP        StandardAEIdleUPP = NewAEIdleProc(StandardAEIdleProc);
  84.  
  85. static pascal Boolean
  86. StandardAEIdleProc(EventRecord *theEvent, long * /* sleepTime */, RgnHandle * /* mouseRgn */)
  87.     {
  88.     HandleEvent(theEvent);            //    First, always hand event off to our event handling code
  89.     return false;                    
  90.     }
  91.  
  92.  
  93. OSErr
  94. ForEachDocumentInList(AEDescList documentList,EachDocumentProcPtr documentProc,void * documentParam)
  95.     {
  96.     long                documentCount,documentIndex;
  97.     DescType            returnedType;
  98.     Size                actualSize;
  99.     LetterDescriptor    theLetterDesc;
  100.     AEKeyword            keyword;
  101.     OSErr                err;
  102.  
  103.     if ((err = AECountItems(&documentList,&documentCount)) != noErr)
  104.         return err;
  105.     
  106.     for (documentIndex=1; documentIndex <= documentCount; documentIndex++)
  107.         {
  108.         //    What kind of document is it?
  109.         if ((err = AESizeOfNthItem(&documentList,documentIndex,&returnedType,&actualSize)) != noErr)
  110.             return err;
  111.         
  112.         //    Is it an AOCE letter?
  113.         if (returnedType == typeLetterSpec)
  114.             {
  115.             //    It’s a letter
  116.             theLetterDesc.onDisk = false;
  117.             err = AEGetNthPtr(&documentList,documentIndex,typeLetterSpec,&keyword,&returnedType,
  118.                                 (Ptr) &theLetterDesc.u.mailboxSpec, sizeof(theLetterDesc.u.mailboxSpec),&actualSize);
  119.             }
  120.         else
  121.             {
  122.             //    It’s just a normal document file
  123.             theLetterDesc.onDisk = true;
  124.             err = AEGetNthPtr(&documentList,documentIndex,typeFSS,&keyword,&returnedType,
  125.                                 (Ptr) &theLetterDesc.u.fileSpec,sizeof(theLetterDesc.u.fileSpec),&actualSize);
  126.             }
  127.             
  128.         if (err == noErr)
  129.             (*documentProc)(&theLetterDesc,documentParam);
  130.         else
  131.             break;
  132.         }
  133.     
  134.     return    err;
  135.     }
  136.  
  137.     
  138. pascal OSErr
  139. HandleOpenApplication(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  140.     {
  141.     OSErr    err;
  142.     
  143.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  144.         return err;
  145.  
  146.     return(CreateNewDocument());
  147.     }
  148.  
  149.  
  150. pascal OSErr
  151. HandleOpenDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  152.     {
  153.     AEDescList            documentList;
  154.     OSErr                err;
  155.     
  156.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  157.         return err;
  158.  
  159.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  160.         return err;
  161.  
  162.     err = ForEachDocumentInList(documentList,&OpenDocument,nil);
  163.     (void) AEDisposeDesc(&documentList);
  164.     return err;
  165.     }
  166.  
  167.  
  168. pascal OSErr
  169. HandlePrintDocuments(AppleEvent *theAppleEvent,AppleEvent * /*reply*/,long /*refCon*/)
  170.     {
  171.     AEDescList            documentList;
  172. #if    qUseQuickDrawGX
  173.     AEDescList            desktopPrinterList;
  174.     FSSpec                thePrinterFSSpec;
  175.     Boolean                draggedToDesktopPrinter = false;
  176. #endif
  177.     OSErr                err;
  178.     
  179.     if ((err = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&documentList)) != noErr)
  180.         return err;
  181.  
  182. #if    qUseQuickDrawGX
  183.     if (noErr == AEGetAttributeDesc(theAppleEvent,keyOptionalKeywordAttr,typeAEList,&desktopPrinterList))
  184.         draggedToDesktopPrinter = true;
  185. #endif
  186.  
  187.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  188.         return err;
  189.  
  190. #if    qUseQuickDrawGX
  191.     if (draggedToDesktopPrinter)
  192.         {
  193.         DescType            returnedType;
  194.         Size                actualSize;
  195.         AEKeyword            keyword;
  196.  
  197.         err = AEGetNthPtr(&desktopPrinterList,1,typeFSS,&keyword,&returnedType,
  198.                             (Ptr) &thePrinterFSSpec,sizeof(thePrinterFSSpec),&actualSize);
  199.  
  200.         (void) AEDisposeDesc(&desktopPrinterList);
  201.         }
  202.     err = ForEachDocumentInList(documentList,&PrintDocument,draggedToDesktopPrinter ? &thePrinterFSSpec : NULL);
  203.     (void) AEDisposeDesc(&documentList);
  204. #else
  205.     err = ForEachDocumentInList(documentList,&PrintDocument,nil);
  206. #endif
  207.  
  208.     return err;
  209.     }
  210.  
  211.  
  212. pascal OSErr
  213. HandleQuitApplication(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  214.     {
  215.     OSErr    err;
  216.     
  217.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  218.         return err;
  219.  
  220.     gDone = QuitApplication();
  221.     
  222.     if (gDone)
  223.         return noErr;
  224.     else
  225.         return userCanceledErr;
  226.     }
  227.  
  228.  
  229. ////////////////////////////////////////////////////////////////////////
  230. //
  231. //    Display Manager Suite is “Under Construction”
  232. //
  233. //    To Do: Check ERS for Display Manager events
  234.  
  235. pascal OSErr
  236. HandleSystemConfigNotice(AppleEvent *theAppleEvent,AppleEvent * /* reply */,long /* refCon */)
  237.     {
  238.     AEDescList    displayList;
  239.     long        displayCount,displayIndex;
  240.     OSErr        err;
  241.     
  242.     DebugStr((StringPtr) "\pGot a Display Manager Event!");
  243.     
  244.     if ((err = AEGetParamDesc(theAppleEvent,kAEDisplayNotice,typeAEList,&displayList)) != noErr)
  245.         return err;
  246.     
  247.     if ((err = CheckAppleEventForMissingParams(theAppleEvent)) != noErr)
  248.         return err;
  249.     
  250.     if ((err = AECountItems(&displayList,&displayCount)) != noErr)
  251.         return err;
  252.  
  253.     for (displayIndex = 1; displayIndex<=displayCount; displayIndex++)
  254.         {
  255.         DebugStr((StringPtr) "\pProcessing a display");
  256.         
  257.         //    Gather up all the old and new display rectangles into an oldDeskRegion and a newDeskRegion
  258.         }
  259.  
  260.     //    run through all windows calling keeping all windows which were onscreen in the oldDeskRegion
  261.     //    onscreen in the new world. We should really be calling a method to do this so that windows
  262.     //    can individually deal with things in a manner which they can override.
  263.  
  264.  
  265.     return errAEEventNotHandled;    //    we really don’t handle this yet...
  266.     }
  267.  
  268. #if    qInlineInputAware
  269.  
  270. pascal OSErr
  271. HandleTextServicesUpdateActiveInputArea(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  272.     {
  273.     DebugStr("\pTextServicesUpdateActiveInputArea");
  274.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  275.     }
  276.  
  277. pascal OSErr
  278. HandleTextServicesPos2Offset(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  279.     {
  280.     DebugStr("\pTextServicesPos2Offset");
  281.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  282.     }
  283.     
  284. pascal OSErr
  285. HandleTextServicesOffset2Pos(AppleEvent *theAppleEvent,AppleEvent *reply,long refCon)
  286.     {
  287.     DebugStr("\pTextServicesOffset2Pos");
  288.     return errAEEventNotHandled;            //    we really don’t handle this yet...
  289.     }
  290.  
  291. #endif
  292.